package training.project.app.repository.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import training.project.app.domain.FlightClient;
import training.project.app.repository.FlightClientDao;
import training.project.app.repository.base.AbstractHibernateDao;

@Repository
@Transactional
public class FlightClientRepository extends
		AbstractHibernateDao<FlightClient, Integer> implements FlightClientDao
	{
		@Override
		public FlightClient findById(Integer clientId)
			{
				Criteria cr = getSession().createCriteria(FlightClient.class,
						"FlightClient").add(Restrictions.eq("id", clientId));
				return (FlightClient) cr.uniqueResult();
			}

		@SuppressWarnings("unchecked")
		public List<FlightClient> getFlightClients(Integer userId)
			{
				Criteria crit = getSession().createCriteria(FlightClient.class,
						"flightClient");
				crit.add(Restrictions.eq("user.userId", userId));
				return (List<FlightClient>) crit.list();
			}

		@Override
		public void purchaseFlight(FlightClient user)
			{
				user.setPurchased(true);
			}
	}
